package com.example.iqiyitest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.iqiyitest.activity.DetailActivity;
import com.example.iqiyitest.activity.DetailForMovieActivity;
import com.example.iqiyitest.activity.MainActivityForMovie;
import com.example.iqiyitest.activity.ShaixuanActivity;
import com.iqiyi.movieksdk.sdk.exported.Constants;
import com.iqiyi.movieksdk.sdk.exported.HttpResult;
import com.iqiyi.movieksdk.sdk.exported.IMsgReceiver;
import com.iqiyi.movieksdk.sdk.exported.MovieSdk;
import com.iqiyi.movieksdk.sdk.exported.OnResponseListener;
import com.iqiyi.movieksdk.sdk.exported.OrderBy;


public class MainActivity extends Activity implements IMsgReceiver {

    private static final String TAG ="MainActivity" ;
    private MovieSdk movieSdk;
    private String host="192.168.1.89";//正式
    private long testQipuId=723849600;//仙球大战
    private TextView text;
    private ImageView image;	

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_home);
        setContentView(R.layout.activity_main);
        text = (TextView) findViewById(R.id.tv);	
        image = (ImageView) findViewById(R.id.iv);
        movieSdk = MovieSdk.init(getApplication(), com.example.iqiyitest.base.Constants.serviceUrl, new MovieSdk.OnInitListener() {
            @Override
            public void initOver(int code) {
                Toast.makeText(MainActivity.this, "init code:"+code, Toast.LENGTH_SHORT).show();
            }
        });
        movieSdk.addMsgReceiver(this);	

    }	

    @Override
    protected void onDestroy() {
        super.onDestroy();
        movieSdk.removeMsgReceiver(this);
    }

    // 1.初始化
    public void init1(View v){
        movieSdk.sdk_init(new OnResponseListener() {
            @Override
            public void onResponse(HttpResult result) {

                if (result.code == HttpResult.CODE_OK) {
                    Log.d(TAG,"init device success"+result.msg);
                    Toast.makeText(MainActivity.this, "init device success"+result.data, Toast.LENGTH_SHORT).show();
                }else{
                    Log.d(TAG,"init device failed"+result.code+" "+result.msg);
                    Toast.makeText(MainActivity.this, "init device failed "+result.code+result.msg, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void reinit(View v){
        movieSdk.sdk_retryInit();

    }
    // 2.分类标签
    public void init2(View v){
//        movieSdk.sdk_movieLabel(new OnResponseListener() {
//            @Override
//            public void onResponse(HttpResult result) {
//                Log.d(TAG,"sdk_movieLabel response"+result.data);
//                text.setText(result.data);
//            }
//        });

    	startActivity(new Intent(MainActivity.this,MainActivityForMovie.class));
    }
    //3.分类获取影片
    public void init3(View v){	
      //全部，动作，惊悚... 取type:leixing
      //全部，内地,取type:diqu
    //取type:shijian
    	startActivity(new Intent(MainActivity.this,DetailActivity.class));
//        movieSdk.sdk_listMovieByLabel("全部","全部","全部", OrderBy.ORDER_XIAZAI,false,1,10,new OnResponseListener() {
//            @Override
//            public void onResponse(HttpResult result) {
//                Log.d(TAG,"sdk_listMovieByLabel:"+result.data);
//                text.setText(result.data);
//            }
//        });
    }
    //4.影片详情
    public void init4(View v){
    	startActivity(new Intent(MainActivity.this,ShaixuanActivity.class));
//        movieSdk.sdk_movieDetail(testQipuId, new OnResponseListener() {
//            @Override
//            public void onResponse(HttpResult result) {
//                Log.d(TAG,result.toString());
//                Log.d(TAG,result.data);
//                startActivity(new Intent(MainActivity.this,DetailForMovieActivity.class).putExtra("data", result.data));
//                text.setText(result.data);
//            }
//        });
    }
    public void init5(View v){
    	
    }
    //6.影片推荐
    public void init6(View v){
        movieSdk.sdk_getRelatedMovieRecom(testQipuId, 10,new OnResponseListener() {
            @Override
            public void onResponse(HttpResult result) {
                Log.d(TAG,result.data);
                text.setText(result.data);
            }
        });
    }
    //7.首页
    public void init7(View v){
        movieSdk.sdk_homePage(1, 20, new OnResponseListener() {
            @Override
            public void onResponse(HttpResult result) {
                Log.d(TAG,result.data);
                text.setText(result.data);
            }
        });
    }
    //8.搜索影片
    public void init8(View v){
        movieSdk.sdk_search("战狼2", new OnResponseListener() {
            @Override
            public void onResponse(HttpResult result) {
                Log.d(TAG,result.data);
                text.setText(result.data);
            }
        });
    }
    //9.首字母检索影片
    public void init9(View v){
        movieSdk.sdk_movieIndex("yingba", 1, 10, new OnResponseListener() {
            @Override	 
            public void onResponse(HttpResult result) {	
                Log.d(TAG,result.data);
                text.setText(result.data);	
            }
        });
    }
    //10.删除设备
    public void init10(View v){
        movieSdk.sdk_deviceDelete(new OnResponseListener() {
            @Override
            public void onResponse(HttpResult result) {
                Log.d(TAG," "+result.data);
                text.setText(" "+result.data);
            }
        });
    }
    //11.设备列表
    public void init11(View v){
        movieSdk.sdk_deviceList(new OnResponseListener() {
            @Override
            public void onResponse(HttpResult result) {
                Log.d(TAG,""+result.data);
                text.setText(" "+result.data);
            }
        });
    }
    //跳转
    public void init12(View v){
        Intent intent=new Intent(this, PlayerActivity.class);
        intent.putExtra("qipuId",testQipuId);
        intent.putExtra("playType", Constants.MODE_OLD);
        startActivity(intent);
    }
    //跳转
    public void init13(View v){
        Intent intent=new Intent(this, PlayerActivity.class);
        intent.putExtra("qipuId",testQipuId);
        intent.putExtra("playType", Constants.MODE_NEW);
        startActivity(intent);
    }

    @Override
    public void onReceived(final String s) {
        Toast.makeText(this, "msg:"+s, Toast.LENGTH_SHORT).show();
        Log.d(TAG,"msg"+s);
        text.post(new Runnable() {
            @Override
            public void run() {
                text.setText("收到消息："+s);
            }
        });

    }
}